package controller

import (
	"context"
	v1 "itam/api/v1"
	"itam/internal/model"
	"itam/internal/service"
)

type cMiddleware struct{}

var Md = cMiddleware{}

func (c *cMiddleware) Create(ctx context.Context, req *v1.MiddlewareCreateReq) (res *v1.MiddlewareCreateRes, err error) {
	out, err := service.Middleware().Create(ctx, model.MiddlewareCreateInput{
		Name:      req.Name,
		IpAddress: req.IpAddress,
		Port:      req.Port,
		EnvId:     req.EnvId,
	})
	if err != nil {
		return nil, err
	}
	return &v1.MiddlewareCreateRes{
		Id: out.Id,
	}, nil
}

func (c *cMiddleware) Delete(ctx context.Context, req *v1.MiddlewareDeleteReq) (res *v1.MiddlewareDeleteRes, err error) {
	err = service.Middleware().Delete(ctx, req.Id)
	return
}

func (c *cMiddleware) Update(ctx context.Context, req *v1.MiddlewareUpdateReq) (res *v1.MiddlewareUpdateRes, err error) {
	err = service.Middleware().Update(ctx, model.MiddlewareUpdateInput{
		Id:        req.Id,
		IpAddress: req.IpAddress,
		Port:      req.Port,
		EnvId:     req.EnvId,
	})
	if err != nil {
		return nil, err
	}
	return
}

func (c *cMiddleware) GetList(ctx context.Context, req *v1.MiddlewareGetListReq) (res *v1.MiddlewareGetListRes, err error) {
	out, err := service.Middleware().GetList(ctx, model.MiddlewareGetListInput{
		Name:      req.Name,
		IpAddress: req.IpAddress,
		Port:      req.Port,
		EnvId:     req.EnvId,
		Page:      req.Page,
		Size:      req.Size,
	})
	if err != nil {
		return nil, err
	}
	return &v1.MiddlewareGetListRes{
		MiddlewareList: out.List,
		Page:           out.Page,
		Size:           out.Size,
		Total:          out.Total,
	}, nil
}
